package com.zl.learning.io;

import com.google.gson.Gson;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.Properties;
import java.util.Set;

public class TestFIle {
    private Gson gson = new Gson();

    @Test
    public void test1() {
        FileSystem fs = FileSystems.getDefault();
        Set<String> views = fs.supportedFileAttributeViews();
        for (String view : views) {
            System.out.println(view);
        }
    }

    @Test
    public void test2() {
        BasicFileAttributes attr = null;
        Path path = Paths.get("D:", "snake2.html");
        try {
            attr = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("File size: " + attr.size());
        System.out.println("File creation time: " + attr.creationTime());
        System.out.println("File was last accessed at: " + attr.lastAccessTime());
        System.out.println("File was last modified at: " + attr.lastModifiedTime());
        System.out.println("Is directory? " + attr.isDirectory());
        System.out.println("Is regular file? " + attr.isRegularFile());
        System.out.println("Is symbolic link? " + attr.isSymbolicLink());
        System.out.println("Is other? " + attr.isOther());
    }

    @Test
    public void test3() {
        Path path = Paths.get("D:/snake2.html");
        try {
            GroupPrincipal group = path.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByGroupName("apressteam");
            Files.getFileAttributeView(path, PosixFileAttributeView.class).setGroup(group);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4() {
        try {
//            Files.createDirectories(Paths.get("demo","demo","test"));
            Path p = Paths.get("demo", "demo");
            DirectoryStream<Path> ts = Files.newDirectoryStream(p);
            ts.forEach(System.out::println);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test5() {
        String default_tmp = System.getProperty("java.io.tmpdir");
        System.out.println(default_tmp);
    }

    @Test
    public void test6() throws IOException {
        Properties prop = new Properties();
        prop.load(new InputStreamReader(new URL("http://localhost/bitcoin.cfg").openStream(), StandardCharsets.UTF_8));
        prop.forEach((k, v) -> System.out.println(k + "----" + v));
    }

    @Test
    public void test7() throws IOException {

        Files.walkFileTree(Paths.get("demo2"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
                if (e != null) {
                    throw e;
                }
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }

    @Test
    public void test8() throws IOException {
        Path base = Paths.get("src/main/java");
        Path p1 = base.resolve(Paths.get("com/zl/io/TestFile.java"));
        System.out.println(p1.toString());
        String val = new String(Files.readAllBytes(p1));
        System.out.println(val);
    }

    @Test
    public void test9() throws IOException {
        Path path = Paths.get("person.xml");
        BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
        System.out.println(gson.toJson(attr));
        FileOwnerAttributeView foa = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
        System.out.println(foa.getOwner().getName());
        PosixFileAttributes pfa = Files.readAttributes(path, PosixFileAttributes.class);
        System.out.println(gson.toJson(pfa));
    }

    @Test
    public void test10() {
        FileSystem fs = FileSystems.getDefault();
        Set<String> views = fs.supportedFileAttributeViews();
        for (String view : views) {
            System.out.println(view);
        }
    }

    @Test
    public void test11() {
        String default_tmp = System.getProperty("java.io.tmpdir");
        System.out.println(default_tmp);
    }

    @Test
    public void test12() throws IOException {
        Files.copy(Paths.get("demo"), Paths.get("demo2"), StandardCopyOption.REPLACE_EXISTING);
    }

    @Test
    public void test13() throws IOException {

        final Path source = Paths.get("D:\\data\\cache\\imsi-1611246177400.zip.ing");
        Files.move(source, Paths.get("D:\\data\\producer", source.getFileName().toString()));
    }

    @Test
    public void test14() throws IOException {
        copy(Paths.get("demo"), Paths.get("demo3"));
    }

    @Test
    public void test15() {
        Path p1 = Paths.get("demo", "demo");
        Path p2 = Paths.get("demo", "demo", "demo4", "nio-data.txt");
        Path p3 = Paths.get("data/data2");
        System.out.println(p3.resolve(p1.relativize(p2)));
    }

    @Test
    public void test16() throws IOException {
        Files.list(Paths.get("D:\\data\\cache")).filter(r -> r.toString().endsWith(".tmp")).forEach(System.out::println);
    }

    @Test
    public void test17() throws IOException {
        long start = System.currentTimeMillis();
        final Path source = Paths.get("D:\\data\\test", "galaxy130_rc3.tar.xz");
        final Path target = Paths.get("D:\\data\\test\\test", "galaxy130_rc3.tar.xz");
//        Files.move(target, source);
        Files.move(source, target);
//        Files.copy(source, target);
//        Files.delete(target);

        long end = System.currentTimeMillis();
        System.out.println("const+" + (end - start));
    }

    @Test
    public void test18() {
        File file = new File("D:\\workspace\\JavaNotes\\io");
        treeFile(file, 0);
    }

    private void treeFile(File file, int index) {
        String sb = "----".repeat(Math.max(0, index)) + file.getName();
        System.out.println(sb);
        if (file.isDirectory()) {
            final File[] files = file.listFiles();
            for (File file1 : files) {
                treeFile(file1,index+1);
//                treeFile(file1, ++index);
//                --index;
            }
        }
    }

    private void copy(Path from, Path to) throws IOException {
        Files.walkFileTree(from, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                Path newDidr = to.resolve(from.relativize(dir));
                Files.copy(dir, newDidr);
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Path newFile = to.resolve(from.relativize(file));
                Files.copy(file, newFile);
                return super.visitFile(file, attrs);
            }
        });
    }
}
